<template>
    <!-- 定义两个具名插槽，用来插入图片和文字 -->
    <div class="tab-bar-item" @click="itemClick">
        <div v-if="!isActive">
            <slot name="item-icon"></slot>
        </div>
        <div v-else>
            <slot name="item-icon-active"></slot>
        </div>
        <div :style="activeStyle">
            <slot name="item-text"></slot>
        </div>
    </div>
</template>


<script>
    export default {
        name: 'TabBarItem',
        data(){
            return{
                
            }
        },
        props:{
            path: String,
            activeColor:{
                path:String,
                default: 'red'
            }
        },
        methods:{
            itemClick(){
                // 解决双击报错问题
                if(this.$route.path == this.path){
                    return
                }
                // 切换页面
                this.$router.replace(this.path)
            }
        },
        computed:{
            isActive(){
                // 匹配到当前页面，返回true
                return this.$route.path.indexOf(this.path)!== -1;
            },
            activeStyle(){
                // 根据isActive 的结果，随之变化
                return this.isActive ? {color:this.activeColor} : {}
            }
        }
    }
</script>


<style>
@import url('../assets/style/TabBarItem.css');
</style>